package db

import (
	"database-platform/dal"
	"database-platform/logger"
	"database-platform/model"
	"fmt"
	"time"
)

func ListInstanceRecords(instance *model.Instance, page, pageSize int64, search string, showCluster, showServer bool) ([]*model.Instance, uint64, error) {
	var query = dal.GORM
	var records []*model.Instance
	var count uint64

	if instance.Hostname != "" {
		query = query.Where("hostname = ?", instance.Hostname)
	}
	if instance.IP != "" {
		query = query.Where("ip = ?", instance.IP)
	}
	if instance.Role != "" {
		query = query.Where("role = ?", instance.Role)
	}
	if instance.DBType != "" {
		query = query.Where("db_type = ?", instance.DBType)
	}
	if instance.ClusterID != nil {
		query = query.Where("cluster_id = ?", instance.ClusterID)
	}
	if instance.ShardNum != nil {
		query = query.Where("shard_num = ?", instance.ShardNum)
	}
	if instance.DBVersion != "" {
		query = query.Where("db_version = ?", instance.DBVersion)
	}
	if instance.DeployType != "" {
		query = query.Where("deploy_type = ?", instance.DeployType)
	}
	if instance.Env != "" {
		query = query.Where("env = ?", instance.Env)
	}
	if instance.Status != "" {
		query = query.Where("status = ?", instance.Status)
	}

	if search != "" {
		query = query.Where("hostname like ?", "%"+search+"%").
			Or("ip like ?", "%"+search+"%")
	}

	if page > 0 && pageSize > 0 {
		offset := (page - 1) * pageSize
		query = query.Offset(offset).Limit(pageSize)
	}

	if showCluster {
		query = query.Preload("Cluster")
	}

	if showServer {
		query = query.Preload("Server")
	}

	err := query.Find(&records).Count(&count).Error
	if err != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.instance.ListInstanceRecords sql execute err, err message is %s, instance: %v", err, instance)
		logger.Logger.Error(msg)
		return nil, 0, err
	}
	return records, count, nil
}

func GetInstanceRecordById(id int) (record *model.Instance, err error) {
	record = &model.Instance{}
	tx := dal.GORM.Where("id = ?", id).First(&record)
	if tx.Error != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.instance.GetInstanceRecordById.sql.execute.err.message.%s.id.%v", tx.Error, id)
		logger.Logger.Error(msg)
		return nil, tx.Error
	}

	return record, nil
}

// InsertInstanceRecord 插入实例记录
func InsertInstanceRecord(instance *model.Instance) error {
	instance.UpdateTime = time.Now()
	instance.CreateTime = time.Now()
	err := dal.GORM.Create(instance).Error
	if err != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.instance.InsertInstanceRecord sql execute err, err message is %s, instance: %v", err, instance)
		logger.Logger.Error(msg)
		return err
	}
	return nil
}

// UpdateInstanceRecord 更新实例记录
func UpdateInstanceRecord(id int, instance *model.Instance) error {
	instance.UpdateTime = time.Now()
	instance.ID = int64(id)
	err := dal.GORM.Save(instance).Error
	if err != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.instance.UpdateInstanceRecord sql execute err, err message is %s, instance: %v", err, instance)
		logger.Logger.Error(msg)
		return err
	}
	return nil
}

// DeleteInstanceRecord 删除实例记录
func DeleteInstanceRecord(id int) error {
	err := dal.GORM.Delete(&model.Instance{}, id).Error
	if err != nil {
		msg := fmt.Sprintf("【DB.LOG】 db.instance.DeleteInstanceRecord sql execute err, err message is %s, id: %d", err, id)
		logger.Logger.Error(msg)
		return err
	}
	return nil
}
